package com.utilities.fastaUtilities;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.RandomAccessFile;

public class ReverseComplement {

	StringBuffer strBuff = new StringBuffer();
	public static void main(String args[]) throws Exception
	{
		if (args.length == 0 )
		{
			args = new String[4];
			// input file
			args[0] = "C:/database/contigTest.txt";
			args[1] = "C:/database/contigTestReverse.txt";
		}
		
		FileWriter fw = new FileWriter(new File(args[1]));
		File f = new File(args[0]);
        FileReader fr = new FileReader(f);
        BufferedReader bufferedReader = new BufferedReader(fr);
        String str = bufferedReader.readLine();
        fw.write(str + "\n");
        bufferedReader.close();
        fr.close();
        fr = new FileReader(f);
        
        RandomAccessFile accessFile = new RandomAccessFile(f, "r");
        StringBuffer buffer = new StringBuffer();
        for (long length = f.length() - 1; length > str.length() ; length--)
        {
        	accessFile.seek(length);
        	char ch = (char)accessFile.readByte();
        	if (ch != '\n' && ch != '\r')
        	{
        		switch(ch)
        		{
        		case 'A' : ch = 'T';
        					break;
        		case 'a' : ch = 't';
        					break;
        		case 'C' : ch = 'G';
        					break;
        		case 'c' : ch = 'g';
        				    break;
        		case 'T' : ch = 'A';
        					break;
        		case 't' : ch = 'a';
        					break;
        		case 'G' : ch = 'C';
        					break;
        		case 'g' : ch = 'c';
        					break;
        		}
        		buffer.append(ch);
            	if (buffer.length() == 60)
            	{
            		fw.write(buffer.toString() + "\n");
            		buffer = new StringBuffer();
            	}
        	}
        }
        fw.flush();
        fw.close();
	}
}
